package com.neo.impatient.C03_array.C03_04_transformArray

import scala.collection.mutable.ArrayBuffer

object TransformArray {

  def main(args: Array[String]): Unit = {


    val a = ArrayBuffer[Int]()
    a ++=ArrayBuffer(5,6,7,8,9)
    println(a)

    val b = Array(1,2,3,4)

    println("利用for推导式,通过转换生成一个全新的集合\n" +
      "如果for迭代的是数组缓冲，生成的也是数组缓冲\n如果是数组，生成的也是数组\n" +
      "yield 每次迭代对应一个元素")
    val result = for(i <- a) yield i * 2
    println(result)

    val result1 = for(i <- b) yield  i * 2
    for(i <- result1)
    println(i)


    println("通过守卫，过滤我们期望的元素")
    val result2 = for(i <- a if i % 2 == 0) yield 2 * i
    println(result2)

    println("通过filter，过滤我们期望的元素")
    val resultFilter = a.filter(_ % 2 == 0).map( 2 * _)
    println(resultFilter)
    val resultFilter1 = a.filter(_ % 2 == 0) map( 2 * _)
    println(resultFilter1)

  }
}
